<script setup lang="ts">
// 导入loginAPI
import { useUserStore } from '@/stores/user'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
// 提示弹窗和样式
import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'





const userStore = useUserStore()
// console.log(userStore.code);


const form = ref({
  nickname: '666666',
  password: '666666',
})
// const { nickname, password } = form.value
const rules = {
  nickname: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },//validator: validatepassword
    { min: 6, max: 14, message: '密码长度为6-14个字符', trigger: 'blur' },
  ],
}
const formRef = ref(null)
const router = useRouter()
// userStore.getUserInfo({ nickname, password })
const submitForm = () => {
  const { nickname, password } = form.value

  // 解构上传数据

  formRef.value.validate(async (valid) => {
    //   vaild为true表示所有表单验证正确
    console.log(valid)
    if (valid) {
      await userStore.getUserInfo({ nickname, password })



      if (userStore.code === 200) {
        // 提示用户
        ElMessage({ type: 'success', message: '登录成功' })
        router.replace({ path: '/friend/list' })
      } else {
        ElMessage({ type: 'error', message: '账号或密码错误' })
      }
      // await XXX({{password, checkpassword}})
    }
  })
}



</script>
<template>
  <!-- 登录区 -->
  <div class="register">
    <el-form ref="formRef" :model="form" status-icon :rules="rules" label-width="5vw" class="demo-form"
      label-position="right" size="large">
      <div class="register-box">
        <el-form-item prop="nickname" label="账户">
          <el-input v-model="form.nickname" class="nickname" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" autocomplete="off" class="password" />
        </el-form-item>
        <el-form-item>
          <el-button :plain="true" type="primary" @click="submitForm" class="denglu">登录</el-button>
          <div @click="$router.push('/register')" class="gozuce">没有账号？去注册</div>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>


<style lang="scss" scoped>
// 登录区域

.register {
  position: relative;
  width: 100vw;
  height: 100vh;
  background: #fff url("@/assets/images/01-logo.png") no-repeat -20vw;
  background-size: 48vw 48vw;
  transition: all 1s;

  &:hover {
    background-position: 0vw 0;
  }

  .demo-form {
    width: 30vw;
    height: 20vw;
    position: absolute;
    top: 30vh;
    right: 30vw;
    border-radius: 2vw;
    border: 2px solid #eae8e5;
    transition: all .5s;

    &:hover {
      transform: translate3d(0, -3px, 0);
      box-shadow: 0 3px 8px rgb(0 0 0 / 20%);
    }

    .register-box {
      margin: 0 auto;
      width: 20vw;
      margin-top: 4vw;

     
      .denglu {
        float: left;
        width: 15vw;
      }

      .gozuce {
        color: #00a895;

        &:hover {
          opacity: 0.6;
        }
      }

    }
  }
}</style>